package com.zl.customer.web;

import com.zl.customer.pojo.Customer;
import com.zl.customer.service.CustomerService;
import com.zl.customer.service.impl.CustomerServiceImpl;
import com.zl.customer.util.DateUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author suke
 * @create 2021-07-09 14:59
 */
@WebServlet(name = "AddCustomerServlet",urlPatterns = "/addCustomer.action")
public class AddCustomerServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //获取请求参数
        String cname = request.getParameter("cname");
        String gender = request.getParameter("gender");
        String birthday = request.getParameter("birthday");
        String cellphone = request.getParameter("cellphone");
        String email = request.getParameter("email");
        String description = request.getParameter("description");

        //把六个参数封装到Customer对象
        Customer customer = new Customer();
        customer.setCname(cname);
        customer.setSex(gender);
        //String转换为java.util.Date
        customer.setBrithday(DateUtils.parseDate(birthday));
        customer.setEmail(email);
        customer.setCellphone(cellphone);
        customer.setDescription(description);

     //调用CustomerService
        CustomerService customerService = new CustomerServiceImpl();
        int rs =  customerService.addCustomer(customer);
        if(rs > 0){ //添加成功
            request.setAttribute("msg","客户添加成功");
        }else{ //添加成功
            request.setAttribute("msg","客户添加失败,请联系管理员!!");
        }
        //跳转到msg.jsp   使用request域, 只能使用转发     错误信息 操作信息 不推荐保存到session
        request.getRequestDispatcher("/jsp/msg.jsp").forward(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
